import java.util.concurrent.CountDownLatch;

public class Demo33 {
    public static void main(String[] args) throws InterruptedException {
        // 10个选手参赛,await就会在10次调用完countDown之后才能继续执行
        CountDownLatch count = new CountDownLatch(10);
        for(int i = 0;i < 9;i++){
            int id = i;
            Thread t = new Thread(()->{
                System.out.println("Thread " + id);
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                // 表示当前这个任务已经执行完毕了
                count.countDown();
            });
            t.start();
        }
        // 等待所有任务执行完毕,才能往下执行
        // a -> all
        count.await();
        System.out.println("所有任务都完成了!");
    }
}
